#! /usr/bin/env python
import string
import sys
import math
from zuluDicts import*

'''Provider
Type
Sand Lots
Currency
Date Open
Date Closed
Open
Close
High
Low
Pips/$
Total'''


class Zuluread(WikiaHeader):
    def __init__(self):
        self.tickerDict = {}
        self.wikialist = []
        self.templist = []
        self.formatZulu_File() #get into comma list
        self.thefileread()  # read into tickerDict
        self.Wikiaformat2()  #LIST wikialist has correct wikia code
        self.winners = 0
        #self.templist = []

    def printstuff(self):
        hosts = self.tickerDict.items()
        #hosts.sort()
        for host,count in hosts:
            print host, ":", count

    def formattingStringForWikia(self,item,color):
        #brackets the -  sign because it means line break in
        #in wikia tables
        greenColor = '''|style="background:#88ee22;"'''
        redColor = '''|style="background:#f0ee22;"'''
        if color == "green":
            item = string.strip(item)
            item = greenColor + "|" + item + "\n"
#            self.winners = self.winners + 1
#            print self.winners
            return item
        if color == "red":
            item = string.strip(item)
            item = redColor + "|" + item + "\n"
            return item
#                item = "|" + item + "\n"
#        |style="background:#88ee22;"| data
###############################################################
        if "-" in item:
            item = string.strip(item)
            item = "|" + "(" + item + ")"+ "\n"
        else:
            item = "|" + item + "\n"
        return item

    def ScaleList_for_correct_pips(self):
        print "aldfjl"
        #BUY,0.5,EUR/USD,2010/01/14 16:11:37,2010/01/14 16:12:55,1.44540,1.44590,25,0,25 Pips,$18.80,342 Pips,$123.00

    def Wikiaformat2(self):
        ########################
        file = open("wikia.txt",'w')
        headerGeneration = headingDict.items()
        for iter,count in headerGeneration:
            x = headingDict[iter]
            x = x + "\n"
            file.write(x)
        file.write("\n")
        #######################
        trades = self.tickerDict.items()
        for iter,count in trades:
           # print self.tickerDict[tradenr]
            self.templist = self.tickerDict[iter]
            self.ScaleList_for_correct_pips()
            print (self.templist)
            ###################
            #self.printcall()
            temp1 = ''
            temp1 = "!" + str(iter) + "\n"
            file.write(temp1)
            for x in range(len(self.templist)):
                temp1 = ''
                item = self.templist[x]
                self.wikialist.append(temp1)
                if x == 0:  #BUY OR SELL
                    color =''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 1:  #LOTS
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 2:  #TICKER
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 3:  #opendate
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 4:  #closedate
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 5:  #open
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 6:  #close
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 7:  #High in pips
                    color = ''
                    item = "|" + item + "\n"
                    file.write(item)
                    self.wikialist.append(item)
                if x == 8:  #Low in pips
                    #SCALE FOR CORRECT NUMERICAL VALUE
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 9:  #pips per lot
                    #SCALE PIPS TO GET CORRECT NUMERICAL RISE
                    color = ''
                    if "-" in item:
                        color = "red"
                    else:
                        color = "green"
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 10:  #Dollar
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 11:  #pipstotal
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 12:  #dollarTotal
                    color = ''
                    item = self.formattingStringForWikia(item,color)   #already has linefeed
                    file.write(item)
                    self.wikialist.append(item)
                    temp1 = "|-" + "\n"
                    file.write(temp1)
                    self.wikialist.append(temp1)
                    file.write("             " + "\n")
                    temp1 = " " + "\n"
                    self.wikialist.append(temp1)
            #############################################
#            print templist
            temp1 = ''
            for x in range(len(self.wikialist)):
                temp1 = self.wikialist[x]
#                file.write(temp1)
        file.close()

#####################################################################################
#####################################################################################
#####################################################################################
class ZuluPipsScale(Zuluread):
    def __init__(self):
        self.tickerDict = {}
        self.wikialist = []
        self.templist = []
        self.formatZulu_File() #get into comma list
        self.thefileread()  # read into tickerDict
        self.Wikiaformat2()  #LIST wikialist has correct wikia code
        #self.templist = []
        self.winners = 0


    def scaleZulu_Pips_to_get_correct_Nominal_movement(self):
        lots = self.templist[1]
        bulkpips = self.templist[9]
        bulkpips = string.split(bulkpips)
#       print bulkpips
        bulkpips = float(bulkpips[0])
        minilots = 10 * float(lots)
        numericalPip = bulkpips/minilots
#       item = "%.2f" % numericalPip
        item = int(numericalPip)
        item = str(item)
        return(item)

    def scaleZulu_Pips_Nominal_movement_LOW(self):
        lots = self.templist[1]
        bulkpips = self.templist[8]
        bulkpips = string.split(bulkpips)
#       print bulkpips
        bulkpips = float(bulkpips[0])
        minilots = 10 * float(lots)
        numericalPip = bulkpips/minilots
#       item = "%.2f" % numericalPip
        item = int(numericalPip)
        item = str(item)
        return(item)

    def scaleZulu_Pips_Nominal_movement_HIGH(self):
        lots = self.templist[1]
        bulkpips = self.templist[7]
        bulkpips = string.split(bulkpips)
#       print bulkpips
        bulkpips = float(bulkpips[0])
        minilots = 10 * float(lots)
        numericalPip = bulkpips/minilots
#       item = "%.2f" % numericalPip
        item = int(numericalPip)
        item = str(item)
        return(item)

    def Wikiaformat2(self):
        ########################
        file = open("wikia.txt",'w')
        headerGeneration = headingDict.items()
        for iter,count in headerGeneration:
            x = headingDict[iter]
            x = x + "\n"
            file.write(x)
        file.write("\n")
        #######################
        trades = self.tickerDict.items()
        for iter,count in trades:
           # print self.tickerDict[tradenr]
            self.templist = self.tickerDict[iter]
#            self.ScaleList_for_correct_pips()  notimplemented
            if "Pips" in (self.templist[9]):
                pass
            else:
                break
            ###################
            #self.printcall()
            temp1 = ''
            temp1 = "!" + str(iter) + "\n"
            file.write(temp1)
            for x in range(len(self.templist)):
                temp1 = ''
                item = self.templist[x]
                self.wikialist.append(temp1)
                temp = self.wikialist
                if x == 0:  #BUY OR SELL
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 1:  #LOTS
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 2:  #TICKER
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 3:  #opendate
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 4:  #closedate
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 5:  #open
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 6:  #close
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 7:  #High in pips
                    color = ''
                    item =  self.scaleZulu_Pips_Nominal_movement_HIGH()
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 8:  #Low in pips
                    #SCALE FOR CORRECT NUMERICAL VALUE
                    color = ''
                    item = self.scaleZulu_Pips_Nominal_movement_LOW()
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 9:  #pips per lot
                    ###########################################################
                    #SCALE PIPS TO GET CORRECT NUMERICAL NOMINAL MOVEMENT(x==1)
                    color = ''
                    item =  self.scaleZulu_Pips_to_get_correct_Nominal_movement()
                    if "-" in item:
                        color = "red"
                    else:
                        color = "green"
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                    ###########################################################
                if x == 10:  #Dollar
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 11:  #pipstotal
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 12:  #dollarTotal
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                    temp1 = "|-" + "\n"
                    file.write(temp1)
                    self.wikialist.append(temp1)
                    file.write("             " + "\n")
                    temp1 = " " + "\n"
                    self.wikialist.append(temp1)
            #############################################
#            print templist
            temp1 = ''
            for x in range(len(self.wikialist)):
                temp1 = self.wikialist[x]
#                file.write(temp1)
        file.close()



#####################################################################################
#####################################################################################
#####################################################################################
class ZuluPipsScale_and_timeHELD(ZuluPipsScale):
    def __init__(self):
        self.tickerDict = {}
        self.wikialist = []
        self.templist = []
        self.formatZulu_File() #get into comma list
        self.thefileread()  # read into tickerDict
        self.Wikiaformat2()  #LIST wikialist has correct wikia code
        #self.templist = []
        self.winners = 0


    def Wikiaformat2(self):
        ########################
        file = open("wikia.txt",'w')
        headerGeneration = headingDict.items()
        for iter,count in headerGeneration:
            x = headingDict[iter]
            x = x + "\n"
            file.write(x)
        file.write("\n")
        #######################
        trades = self.tickerDict.items()
        for iter,count in trades:
           # print self.tickerDict[tradenr]
            self.templist = self.tickerDict[iter]
#            self.ScaleList_for_correct_pips()  notimplemented
            if "Pips" in (self.templist[9]):
                pass
            else:
                break
            ###################
            #self.printcall()
            temp1 = ''
            temp1 = "!" + str(iter) + "\n"
            file.write(temp1)
            for x in range(len(self.templist)):
                temp1 = ''
                item = self.templist[x]
                self.wikialist.append(temp1)
                temp = self.wikialist
                if x == 0:  #BUY OR SELL
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 1:  #LOTS
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 2:  #TICKER
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 3:  #opendate
                    color = ''
                    item = '#'
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 4:  #closedate
                    color = ''
                    item = '#'  #blank out open
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 5:  #open
                    color = ''
                    item = '#'  #blank out open
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 6:  #close
                    color = ''
                    item = '#'
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 7:  #High in pips
                    color = ''
                    item =  self.scaleZulu_Pips_Nominal_movement_HIGH()
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 8:  #Low in pips
                    #SCALE FOR CORRECT NUMERICAL VALUE
                    color = ''
                    item = self.scaleZulu_Pips_Nominal_movement_LOW()
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 9:  #pips per lot
                    ###########################################################
                    #SCALE PIPS TO GET CORRECT NUMERICAL NOMINAL MOVEMENT(x==1)
                    color = ''
                    item =  self.scaleZulu_Pips_to_get_correct_Nominal_movement()
                    if "-" in item:
                        color = "red"
                    else:
                        color = "green"
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                    ###########################################################
                if x == 10:  #Dollar
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 11:  #pipstotal
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                if x == 12:  #dollarTotal
                    color = ''
                    item = self.formattingStringForWikia(item,color)
                    file.write(item)
                    self.wikialist.append(item)
                    temp1 = "|-" + "\n"
                    file.write(temp1)
                    self.wikialist.append(temp1)
                    file.write("             " + "\n")
                    temp1 = " " + "\n"
                    self.wikialist.append(temp1)
            #############################################
#            print templist
            temp1 = ''
            for x in range(len(self.wikialist)):
                temp1 = self.wikialist[x]
#                file.write(temp1)
        file.close()


def main():
    channel = "#forextiming"
    nickname = "zimba23"
    server = "irc.freenode.net"
    port = 6667
#    bot = Zuluread()
    bot = ZuluPipsScale()
#    bot = ZuluPipsScale_and_timeHELD()

#    bot.printstuff()
#    bot.GenerateHeader()  #Generate wikia code


if __name__ == "__main__":
    main()
